package com.sgb.kjwl.view.ui.adapter.base

import android.view.View

abstract class RecyclerViewExpandable<T,G,C>: HDBaseAdapter<SimpleViewHolder>(),
        RecyclerContract.Expandable,
        RecyclerContract.ExpandableContract<T, G, C> {
    lateinit var expandableHelper : ExpandableHelper
    /**
     * Group ���̵���¼�
     */
    var onGroupItemClickListeners: RecyclerContract.OnGroupItemClickListener? = null
    var onGroupItemLongClickListener: RecyclerContract.OnGroupItemLongClickListener? = null

    /**
     * Child ���̵���¼�
     */
    var onChildItemClickListener: RecyclerContract.OnChildItemClickListener? = null
    var onChildItemLongClickListener: RecyclerContract.OnChildItemLongClickListener? = null

    init {
        initExpandableHelper()
    }

    private fun initExpandableHelper(){
        expandableHelper = ExpandableHelper(this)
    }
    protected open fun getChildItemViewType(groupPosition: Int, childPosition: Int): Int {
        return 1
    }

    protected fun getGroupItemViewType(groupPosition: Int): Int {
        return 1
    }

    protected fun getGroupItemId(groupPosition: Int): Long {
        return -1
    }

    protected fun getChildItemId(groupPosition: Int, childPosition: Int): Long {
        return -1
    }

    protected abstract fun onBindGroupViewHolder(holder: SimpleViewHolder?, groupPosition: Int)
    protected abstract fun onBindChildViewHolder(holder: SimpleViewHolder?, groupPosition: Int, childPosition: Int)
    protected abstract fun getGroupLayoutResId(viewType: Int): Int
    protected abstract fun getChildLayoutResId(viewType: Int): Int
    abstract override fun getGroupItem(groupPosition: Int): G?
    abstract override fun getChildItem(groupPosition: Int, childPosition: Int): C?

    /**
     * ����¼�
     *
     * @param view View
     * @param position λ��
     */
    override fun onItemClick(view: View?, position: Int) {
        if (position < 0 || position > getDataItemCount()){
            return
        }
        val (first, second) = expandableHelper.indexOfPosition(position)
        if (getItemViewType(position) > 0) {
            onChildItemClickListener?.onChildItemClick(view, first, second)
        } else if (getItemViewType(position) < 0) {
            onGroupItemClickListeners?.onGroupItemClick(view, first)
        }
    }

    override fun onItemLongClick(view: View?, position: Int) {
        if (position < 0 || position > getDataItemCount()){
            return
        }
        val ofValue = expandableHelper.indexOfValue(position)
        if (ofValue < 0) {
            val (first, second) = expandableHelper.indexOfPosition(position)
            onChildItemLongClickListener?.onChildItemLongClick(view, first, second)
        } else if (ofValue > 0) {
            onGroupItemLongClickListener?.onGroupItemLongClick(view, ofValue)
        }
    }

    override fun onBindDataViewHolder(holder: SimpleViewHolder, position: Int) {
        val lViewType = getItemViewType(position)
        val (first, second) = expandableHelper.indexOfPosition(position)
        if (lViewType > 0) {
            onBindChildViewHolder(holder, first, second)
        } else {
            onBindGroupViewHolder(holder, first)
        }
    }

    override fun getDataItemViewType(position: Int): Int {
        val (first, second) = expandableHelper.indexOfPosition(position)
        return if (second == -1) {
            checkParameters(-getGroupItemViewType(first))
        } else {
            checkParameters(getChildItemViewType(first, second))
        }
    }
    override fun getDataItemCount(): Int {
        return expandableHelper.getItemCount()
    }

    override fun getLayoutResId(viewType: Int): Int {
        return if (viewType > 0) {
            getChildLayoutResId(viewType)
        } else {
            getGroupLayoutResId(viewType)
        }
    }

    override fun getItemId(position: Int): Long {
        val (first, second) = expandableHelper.indexOfPosition(position)
        return if (getItemViewType(position) > 0) {
            getChildItemId(first, second)
        } else {
            getGroupItemId(first)
        }
    }


    /**
     * check View Type
     * @param viewType Vaue
     * @return value
     */
    private fun checkParameters(viewType: Int): Int {
        if (viewType == 0) {
            throw NullPointerException("viewType!=0!")
        }
        return viewType
    }
}
